package com.boot.controller;

import com.boot.model.User;
import io.swagger.annotations.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @Auther: zhengshunzhi
 * @Date: 2018/8/31 10:43
 * @Description: 初始化的controller
 */
@Api(value = "LoginController", description = "用户登入登出接口")
@Controller
public class LoginController {

    @ApiIgnore//忽略这个接口展示api
    @GetMapping(value = "login")
    public String login() {
        return "login";
    }

    /**
     * 功能描述: 直接访问主页
     *
     * @return:
     * @auther: bonree
     * @date: 2018/8/31 10:45
     */
    @ApiOperation(value = "首页跳转", notes = "首页跳转接口")
    @GetMapping(value = {"/", "/index"})
    public String index(Model model) {
        Session session = SecurityUtils.getSubject().getSession();
        if ((User) session.getAttribute("user") == null) {
            return "login";
        }
        User user = (User) session.getAttribute("user");
        model.addAttribute("username", "欢迎" + user.getName() + "光临!请选择你的操作");//user.getName()
        return "index";
    }

    /**
     * 功能描述: 登录页面
     *
     * @param:
     * @param:
     * @return:
     * @auther: bonree
     * @date: 2018/8/31 10:45
     */
    @ApiOperation(value = "用户登录", notes = "用户登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "string"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "string"),
            @ApiImplicitParam(name = "rememeberMe", value = "记住我", required = false, dataType = "boolean"),
            @ApiImplicitParam(name = "vrifyCode", value = "验证码", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数没有填好"),
            @ApiResponse(code = 404, message = "请求路径没有找到"),
    })
    @PostMapping(value = "/loginUser")
    public String login(String username, String password, boolean rememeberMe, String vrifyCode, Model model) {
        Subject subject = SecurityUtils.getSubject();
        //获取用户session(如果当前用户没有常见session的话,true则创建一个并返回,false为返回null)
        Session session = subject.getSession(true);
        //表示当前用户已经登录
        if (!StringUtils.isEmpty(session.getAttribute("user"))) {
            //重定向
            return "redirect:/index";
        }

        if (username == null && password == null) {
            return "login";
        }

        if (username == null) {
            model.addAttribute("msg", "用户名不能为空");
            return "login";
        }

        if (password == null) {
            model.addAttribute("msg", "密码不能为空");
            return "login";
        }

        if (StringUtils.isEmpty(vrifyCode)) {
            model.addAttribute("msg", "验证码不能为空");
            return "login";
        }

        String vrifysCode = (String) session.getAttribute("vrifyCode");
        if (!vrifysCode.equalsIgnoreCase(vrifyCode)) {
            model.addAttribute("msg", "验证码错误");
            return "login";
        }

        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        //是否记住我
        token.setRememberMe(rememeberMe);

        String msg = null;

        try {
            subject.login(token);
            //获取登录用户
            User user = (User) subject.getPrincipal();
            session.setAttribute("user", user);
            //重定向
            return "redirect:/index";
        } catch (IncorrectCredentialsException e) {
            msg = "登录密码错误";
            System.out.println("登录密码错误!!!" + e);
        } catch (ExcessiveAttemptsException e) {
            msg = "登录失败次数过多";
            System.out.println("登录失败次数过多!!!" + e);
        } catch (LockedAccountException e) {
            msg = "帐号已被锁定";
            System.out.println("帐号已被锁定!!!" + e);
        } catch (DisabledAccountException e) {
            msg = "帐号已被禁用";
            System.out.println("帐号已被禁用!!!" + e);
        } catch (ExpiredCredentialsException e) {
            msg = "帐号已过期";
            System.out.println("帐号已过期!!!" + e);
        } catch (UnknownAccountException e) {
            msg = "帐号不存在";
            System.out.println("帐号不存在!!!" + e);
        } catch (UnauthorizedException e) {
            msg = "您没有得到相应的授权！";
            System.out.println("您没有得到相应的授权！" + e);
        } catch (Exception e) {
            System.out.println("出错！！！" + e);
        }
        model.addAttribute("msg", msg);
        return "login";
    }

    /**
     * 功能描述: 登出
     *
     * @param:
     * @return:
     * @auther: bonree
     * @date: 2018/8/31 14:37
     */
    @GetMapping(value = "/logout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        subject.getSession().removeAttribute("user");
        return "login";
    }

    /**
     * 功能描述: 被踢出后跳转的页面
     *
     * @param:
     * @return:
     * @auther: bonree
     * @date: 2018/9/7 14:19
     */
    @GetMapping("/kickout")
    public String kickout() {
        return "kickout";
    }
}
